package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.entity.Project;
import cn.js.sangu.unitStore.entity.User;
import cn.js.sangu.unitStore.service.ProjectService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：研发立项接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("project")
@RestController
public class ProjectController {

    static final String OPERATION_PROJECT = "研发立项";

    @Autowired
    ProjectService projectService;

    @Autowired
    UserService userService;

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize,
                        @RequestParam String projectName,
                        @RequestParam String creator,
                        @RequestParam String recipient){
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(projectName),"project_name",projectName);
        wrapper.eq(StrUtil.isNotBlank(creator),"creator",creator);
        wrapper.like(StrUtil.isNotBlank(recipient),"recipient",recipient);
        wrapper.orderByDesc("id");
        //构建page对象
        Page<Project> param = new Page<>(pageNum, pageSize);
        Page<Project> res = projectService.page(param, wrapper);
        return R.ok(res.setRecords(suppleName(res.getRecords())));
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody Project dto){
        Project project = projectService.saveInfo(dto);
        return R.ok("《"+project.getProjectName()+"》保存成功");
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.UPDATE)
    @PutMapping("update")
    public R update(@RequestBody Project dto){
        projectService.updateInfo(dto);
        return R.ok("修改成功");
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.AUDIT_PASS)
    @PutMapping("audit_pass")
    public R auditPass(@RequestBody Project dto){
        projectService.updateInfo(dto);
        return R.ok("处理成功");
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.AUDIT_REJECT)
    @PutMapping("audit_reject")
    public R auditReject(@RequestBody Project dto){
        projectService.updateInfo(dto);
        return R.ok("处理成功");
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.DELETE)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        projectService.removeById(id);
        return R.ok("删除成功");
    }

    @MyLogsAOP(operation = OPERATION_PROJECT,operaType = LogType.BATCH_DELETE)
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        projectService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

//    @MyLogsAOP(operation = OPERATION_PROJECTUMENT,operaType = LogType.LOOKUP)
    @MyAuthAccess
    @GetMapping("findById/{id}")
    public R findById(@PathVariable Integer id){
        Project project = projectService.getById(id);
        User user = userService.getById(project.getCreator());
        if (null != user){
            project.setCreatorName(user.getName());
        }
        return R.ok(project);
    }

//    @MyLogsAOP(operation = OPERATION_PROJECTUMENT,operaType = LogType.LOOKUP)
    @GetMapping("findByRecent")
    public R findByRecent(){
        LambdaQueryWrapper<Project> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(Project::getCreatTime).last("LIMIT 10");
        return R.ok(suppleName(projectService.list(wrapper)));
    }

    /**
     * 补充创建人+修改人+接收人 名称
     * @param list
     * @return
     */
    private List<Project> suppleName(List<Project> list ){
        for (Project project : list) {
            //创建人
            User author = userService.getById(project.getCreator());
            if (null != author) project.setCreatorName(author.getName());
            //修改人
            User modifier = userService.getById(project.getModifier());
            if (null != modifier) project.setModifierName(modifier.getName());
            //接收人
            User recipient = userService.getById(project.getRecipient());
            if (null != recipient) project.setRecipientName(recipient.getName());
            //接收人
            User auditor = userService.getById(project.getAuditor());
            if (null != auditor) project.setAuditorName(auditor.getName());
        }
        return list;
    }

}
